package com.mars.tukuan.rpc.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mars.base.common.utils.Paginator;
import com.mars.base.service.support.BaseService;
import com.mars.tukuan.dao.mapper.CarStateMapper;
import com.mars.tukuan.dto.CarStateVo;
import com.mars.tukuan.dto.ReportCarInjureMonthTotalDto;
import com.mars.tukuan.dto.ReportUserMonthCheckDto;
import com.mars.tukuan.dto.ReportUserMonthMoneyDto;
import com.mars.tukuan.dto.ReportUserMonthTcDto;
import com.mars.tukuan.entity.CarState;
import com.mars.tukuan.rpc.api.CarStateService;

@Service
public class CarStateServiceImpl extends BaseService<CarState, Long> implements CarStateService {
	
	@Autowired
	private CarStateMapper mapper ;

	@Override
	public PageInfo<CarStateVo> selectPageCarState(Paginator paginator, CarStateVo entity) {
		PageHelper.startPage(paginator.getPage(), paginator.getRows()) ;
		Page<CarStateVo> page = this.mapper.selectPageCarState(paginator, entity) ;
		
		return new PageInfo<CarStateVo>(page) ;
	}

	@Override
	public int compensate(CarState entity) {
		return this.mapper.compensate(entity);
	}

	@Override
	public int follow(CarState entity) {
		return this.mapper.follow(entity);
	}

	@Override
	public PageInfo<CarStateVo> list(Paginator paginator, CarStateVo entity) {
		PageHelper.startPage(paginator.getPage(), paginator.getRows()) ;
		Page<CarStateVo> page = this.mapper.list(paginator, entity) ;
		
		return new PageInfo<CarStateVo>(page) ;
	}

	@Override
	public int deleteByCheckId(Long carCheckId) {
		return this.mapper.deleteByCheckId(carCheckId);
	}

	@Override
	public List<ReportUserMonthMoneyDto> reportUserMonthMoney(ReportUserMonthMoneyDto entity) {
		Map<Long, ReportUserMonthMoneyDto> map = new HashMap<Long, ReportUserMonthMoneyDto>() ;
		
		List<ReportUserMonthMoneyDto> carReportMonthUserMoney = this.mapper.reportUserMonthMoney(entity) ;
		for (ReportUserMonthMoneyDto value : carReportMonthUserMoney) {
			setUserMonthMapData(map, value);
		}
		
		List<ReportUserMonthMoneyDto> list = new ArrayList<ReportUserMonthMoneyDto>() ;
		for (Long key : map.keySet()) {
			list.add(map.get(key)) ;
		}
		
		return list ;
	}
	
	private void setUserMonthMapData(Map<Long, ReportUserMonthMoneyDto> map, ReportUserMonthMoneyDto value) {
		ReportUserMonthMoneyDto reportUserMonthMoneyDto = map.get(value.getUserId()) ;
		if(null == reportUserMonthMoneyDto) {
			setUserMonth(value, value);
			map.put(value.getUserId(), value) ;
		} else {
			setUserMonth(reportUserMonthMoneyDto, value);
			if(null != reportUserMonthMoneyDto.getTotalMoney()) {
				reportUserMonthMoneyDto.setTotalMoney(
						(null == reportUserMonthMoneyDto.getTotalMoney() ? 0 : reportUserMonthMoneyDto.getTotalMoney())
						+
						(null == value.getTotalMoney() ? 0 : value.getTotalMoney()));
			}
			map.put(value.getUserId(), reportUserMonthMoneyDto) ;
		}
	}
	
	private void setUserMonth(ReportUserMonthMoneyDto mapValue, ReportUserMonthMoneyDto value) {
		switch (value.getMonth()) {
		case 1:
			mapValue.setM1(value.getTotalMoney());
			break;
		case 2:
			mapValue.setM2(value.getTotalMoney());
			break;
		case 3:
			mapValue.setM3(value.getTotalMoney());
			break;
		case 4:
			mapValue.setM4(value.getTotalMoney());
			break;
		case 5:
			mapValue.setM5(value.getTotalMoney());
			break;
		case 6:
			mapValue.setM6(value.getTotalMoney());
			break;
		case 7:
			mapValue.setM7(value.getTotalMoney());
			break;
		case 8:
			mapValue.setM8(value.getTotalMoney());
			break;
		case 9:
			mapValue.setM9(value.getTotalMoney());
			break;
		case 10:
			mapValue.setM10(value.getTotalMoney());
			break;
		case 11:
			mapValue.setM11(value.getTotalMoney());
			break;
		case 12:
			mapValue.setM12(value.getTotalMoney());
			break;
		default:
			break;
		}
	}

	@Override
	public List<ReportCarInjureMonthTotalDto> reportCarInjureMonthTotal(ReportCarInjureMonthTotalDto entity) {
		Map<Long, ReportCarInjureMonthTotalDto> map = new HashMap<Long, ReportCarInjureMonthTotalDto>() ;
		
		List<ReportCarInjureMonthTotalDto> reportCarInjureMonthTotalDtos = this.mapper.reportCarInjureMonthTotal(entity) ;
		for (ReportCarInjureMonthTotalDto value : reportCarInjureMonthTotalDtos) {
			setCarInjureMonthMapData(map, value);
		}
		
		List<ReportCarInjureMonthTotalDto> list = new ArrayList<ReportCarInjureMonthTotalDto>() ;
		for (Long key : map.keySet()) {
			list.add(map.get(key)) ;
		}
		
		return list ;
	}
	
	private void setCarInjureMonthMapData(Map<Long, ReportCarInjureMonthTotalDto> map, ReportCarInjureMonthTotalDto value) {
		ReportCarInjureMonthTotalDto reportCarInjureMonthTotalDto = map.get(value.getCarId()) ;
		if(null == reportCarInjureMonthTotalDto) {
			setCarInjureMonth(value, value);
			map.put(value.getCarId(), value) ;
		} else {
			setCarInjureMonth(reportCarInjureMonthTotalDto, value);
			if(null != reportCarInjureMonthTotalDto.getTotal()) {
				reportCarInjureMonthTotalDto.setTotal(
						(null == reportCarInjureMonthTotalDto.getTotal() ? 0 : reportCarInjureMonthTotalDto.getTotal())
						+
						(null == value.getTotal() ? 0 : value.getTotal()));
			}
			map.put(value.getCarId(), reportCarInjureMonthTotalDto) ;
		}
	}
	
	private void setCarInjureMonth(ReportCarInjureMonthTotalDto mapValue, ReportCarInjureMonthTotalDto value) {
		switch (value.getMonth()) {
		case 1:
			mapValue.setM1(value.getTotal());
			break;
		case 2:
			mapValue.setM2(value.getTotal());
			break;
		case 3:
			mapValue.setM3(value.getTotal());
			break;
		case 4:
			mapValue.setM4(value.getTotal());
			break;
		case 5:
			mapValue.setM5(value.getTotal());
			break;
		case 6:
			mapValue.setM6(value.getTotal());
			break;
		case 7:
			mapValue.setM7(value.getTotal());
			break;
		case 8:
			mapValue.setM8(value.getTotal());
			break;
		case 9:
			mapValue.setM9(value.getTotal());
			break;
		case 10:
			mapValue.setM10(value.getTotal());
			break;
		case 11:
			mapValue.setM11(value.getTotal());
			break;
		case 12:
			mapValue.setM12(value.getTotal());
			break;
		default:
			break;
		}
	}

	@Override
	public List<ReportUserMonthCheckDto> reportUserMonthCheck(
			ReportUserMonthCheckDto entity) {
		Map<Long, ReportUserMonthCheckDto> map = new HashMap<Long, ReportUserMonthCheckDto>() ;
		
		List<ReportUserMonthCheckDto> carReportMonthUserMoney = this.mapper.reportUserMonthCheck(entity) ;
		for (ReportUserMonthCheckDto value : carReportMonthUserMoney) {
			setUserMonthCheckMapData(map, value);
		}
		
		List<ReportUserMonthCheckDto> list = new ArrayList<ReportUserMonthCheckDto>() ;
		for (Long key : map.keySet()) {
			list.add(map.get(key)) ;
		}
		
		return list ;
	}
	
	private void setUserMonthCheckMapData(Map<Long, ReportUserMonthCheckDto> map, ReportUserMonthCheckDto value) {
		ReportUserMonthCheckDto reportUserMonthMoneyDto = map.get(value.getUserId()) ;
		if(null == reportUserMonthMoneyDto) {
			setUserMonthCheck(value, value);
			map.put(value.getUserId(), value) ;
		} else {
			setUserMonthCheck(reportUserMonthMoneyDto, value);
			if(null != reportUserMonthMoneyDto.getTotalCheck()) {
				reportUserMonthMoneyDto.setTotalCheck(
						(null == reportUserMonthMoneyDto.getTotalCheck() ? 0 : reportUserMonthMoneyDto.getTotalCheck())
						+
						(null == value.getTotalCheck() ? 0 : value.getTotalCheck()));
			}
			map.put(value.getUserId(), reportUserMonthMoneyDto) ;
		}
	}
	
	private void setUserMonthCheck(ReportUserMonthCheckDto mapValue, ReportUserMonthCheckDto value) {
		switch (value.getMonth()) {
		case 1:
			mapValue.setM1(value.getTotalCheck());
			break;
		case 2:
			mapValue.setM2(value.getTotalCheck());
			break;
		case 3:
			mapValue.setM3(value.getTotalCheck());
			break;
		case 4:
			mapValue.setM4(value.getTotalCheck());
			break;
		case 5:
			mapValue.setM5(value.getTotalCheck());
			break;
		case 6:
			mapValue.setM6(value.getTotalCheck());
			break;
		case 7:
			mapValue.setM7(value.getTotalCheck());
			break;
		case 8:
			mapValue.setM8(value.getTotalCheck());
			break;
		case 9:
			mapValue.setM9(value.getTotalCheck());
			break;
		case 10:
			mapValue.setM10(value.getTotalCheck());
			break;
		case 11:
			mapValue.setM11(value.getTotalCheck());
			break;
		case 12:
			mapValue.setM12(value.getTotalCheck());
			break;
		default:
			break;
		}
	}
	
	
	@Override
	public List<ReportUserMonthTcDto> reportUserMonthTc(ReportUserMonthTcDto entity) {
		Map<Long, ReportUserMonthTcDto> map = new HashMap<Long, ReportUserMonthTcDto>() ;
		
		List<ReportUserMonthTcDto> carReportMonthUserMoney = this.mapper.reportUserMonthTc(entity) ;
		for (ReportUserMonthTcDto value : carReportMonthUserMoney) {
			setUserMonthTcMapData(map, value);
		}
		
		List<ReportUserMonthTcDto> list = new ArrayList<ReportUserMonthTcDto>() ;
		for (Long key : map.keySet()) {
			list.add(map.get(key)) ;
		}
		
		return list ;
	}
	
	private void setUserMonthTcMapData(Map<Long, ReportUserMonthTcDto> map, ReportUserMonthTcDto value) {
		ReportUserMonthTcDto reportUserMonthMoneyDto = map.get(value.getUserId()) ;
		if(null == reportUserMonthMoneyDto) {
			setUserMonthTc(value, value);
			map.put(value.getUserId(), value) ;
		} else {
			setUserMonthTc(reportUserMonthMoneyDto, value);
			if(null != reportUserMonthMoneyDto.getTotalMoney()) {
				reportUserMonthMoneyDto.setTotalMoney(
						(null == reportUserMonthMoneyDto.getTotalMoney() ? 0 : reportUserMonthMoneyDto.getTotalMoney())
						+
						(null == value.getTotalMoney() ? 0 : value.getTotalMoney()));
			}
			map.put(value.getUserId(), reportUserMonthMoneyDto) ;
		}
	}
	
	private void setUserMonthTc(ReportUserMonthTcDto mapValue, ReportUserMonthTcDto value) {
		switch (value.getMonth()) {
		case 1:
			mapValue.setM1(value.getTotalMoney());
			break;
		case 2:
			mapValue.setM2(value.getTotalMoney());
			break;
		case 3:
			mapValue.setM3(value.getTotalMoney());
			break;
		case 4:
			mapValue.setM4(value.getTotalMoney());
			break;
		case 5:
			mapValue.setM5(value.getTotalMoney());
			break;
		case 6:
			mapValue.setM6(value.getTotalMoney());
			break;
		case 7:
			mapValue.setM7(value.getTotalMoney());
			break;
		case 8:
			mapValue.setM8(value.getTotalMoney());
			break;
		case 9:
			mapValue.setM9(value.getTotalMoney());
			break;
		case 10:
			mapValue.setM10(value.getTotalMoney());
			break;
		case 11:
			mapValue.setM11(value.getTotalMoney());
			break;
		case 12:
			mapValue.setM12(value.getTotalMoney());
			break;
		default:
			break;
		}
	}

}
